package tech.abing.spider.task.pipeline;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

//import com.asprise.ocr.Ocr;
import tech.abing.spider.component.Pipeline;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.task.model.Five8Entity;
import tech.abing.spider.util.ProccessableUtil;
import tech.abing.spider.util.SqlUtil;

public class Five8Pipeline implements Pipeline {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static SqlUtil sqlUtil = null;
//	private static Ocr ocr = null;
	
	
	
	public Five8Pipeline() {
		
		if(sqlUtil == null){
			sqlUtil = SqlUtil.getInstance();
		}
		
//		if(ocr == null){
//			Ocr.setUp(); // one time setup
//			Ocr ocr = new Ocr(); // create a new OCR engine
//			ocr.startEngine("eng", Ocr.SPEED_FASTEST); // English
//		}
	}
	
	

	@Override
	public void proccessData(List<Proccessable> objectContainer) {

		if (ProccessableUtil.instanceOfClass(objectContainer, Five8Entity.class)) {// 判断集合类型是否是Five8Entity
			List<Five8Entity> list = ProccessableUtil.convert(objectContainer, Five8Entity.class);// 转换
			
			for(Five8Entity five8 : list){
				if(StringUtils.isNotBlank(five8.getPhone())){
					System.out.println(five8);
					
//					try {
//						String tel = ocr.recognize(new URL[] {new URL(five8.getPhone())},
//								  Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT);
//						
//						five8.setTel(tel);
//					} catch (MalformedURLException e) {
//						e.printStackTrace();
//					} // PLAINTEXT | XML | PDF | RTF
					
				}
				
//				if(StringUtils.isNotBlank(five8.getPhone2())){
//					try {
////						String tel2 = ocr.recognize(new URL[] {new URL(five8.getPhone2())},
////								  Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT);
////						
////						five8.setTel2(tel2);
//					} catch (MalformedURLException e) {
//						e.printStackTrace();
//					} // PLAINTEXT | XML | PDF | RTF
//				}
				
			}
			
			//sqlUtil.batchInsert(sqlUtil.getConn(), Five8Entity.class, list);
		}
		
	}

	@Override
	public void destory() {
		if(sqlUtil != null){
			sqlUtil.closeConn();
		}
		
//		if(ocr != null){
//			ocr.stopEngine();
//			ocr = null;
//		}
	}

}
